// 子模块moduleA路径：store/modules/moduleA.js
export default {
	namespaced: true,
	state: {
		/**
		 * id
		 * image
		 * price
		 * count
		 * state
		 * title
		 *
		 */
		cartList: []
	},
	getters: {
		totalPrice(state) {
			return state.cartList.filter(v => v.state).reduce((p, n) => p + n.count * n.price, 0)
		},
		allState(state) {
			return state.cartList.every(v => v.state)
		}
	},
	mutations: {
		addCart(state, payload) {
			// 1. 查找数据是否存在
			const item = state.cartList.find(v => v.id === payload.id);
			if (item) {
				item.count++
			} else {
				state.cartList.push(payload)
			}
		},
		subCart(state, payload) {
			// 1. 查找数据是否存在
			const item = state.cartList.find(v => v.id === payload.id);
			if (item) {
				item.count--
			}
		},
		deleteCart(state, payload) {
			state.cartList = state.cartList.filter(v => v.id !== payload.id)
		},
		changeState(state, payload) {
			const item = state.cartList.find(v => v.id === payload.id);
			if (item) {
				// 切换状态
				item.state = !item.state
			}
		},
		changeAllState(state, bool) {
			state.cartList.forEach(v => {
				v.state = bool
			})
		}
	},
	actions: {

	}
}